home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 001-100 / 001-025 / 022 / lemacs / window.c < prev    next >
C/C++ Source or Header  |  1995-03-17  |  12KB  |  399 lines

  1. /*
  2.  * Window management. Some of the functions are internal, and some are
  3.  * attached to keys that the user actually types.
  4.  */
  5.  
  6. #include        <stdio.h>
  7. #include        "estruct.h"
  8. #include    "edef.h"
  9.  
  10. /*
  11.  * Reposition dot in the current window to line "n". If the argument is
  12.  * positive, it is that line. If it is negative it is that line from the
  13.  * bottom. If it is 0 the window is centered (this is what the standard
  14.  * redisplay code does). With no argument it defaults to 1. Bound to M-!.
  15.  * Because of the default, it works like in Gosling.
  16.  */
  17. reposition(f, n)
  18.     {
  19.     if (f == FALSE)    /* default to 0 to center screen */
  20.     n = 0;
  21.     curwp->w_force = n;
  22.     curwp->w_flag |= WFFORCE;
  23.     return (TRUE);
  24.     }
  25.  
  26. /*
  27.  * Refresh the screen. With no argument, it just does the refresh. With an
  28.  * argument it recenters "." in the current window. Bound to "C-L".
  29.  */
  30. refresh(f, n)
  31.     {
  32.     if (f == FALSE)
  33.         sgarbf = TRUE;
  34.     else
  35.         {
  36.         curwp->w_force = 0;             /* Center dot. */
  37.         curwp->w_flag |= WFFORCE;
  38.         }
  39.  
  40.     return (TRUE);
  41.     }
  42.  
  43. /*
  44.  * The command make the next window (next => down the screen) the current
  45.  * window. There are no real errors, although the command does nothing if
  46.  * there is only 1 window on the screen. Bound to "C-X C-N".
  47.  */
  48. nextwind(f, n)
  49.     {
  50.     register WINDOW *wp;
  51.  
  52.     if ((wp = curwp->w_wndp) == NULL)
  53.         wp = wheadp;
  54.  
  55.     curwp = wp;
  56.     curbp = wp->w_bufp;
  57.     upmode();
  58.     return (TRUE);
  59.     }
  60.  
  61. /*
  62.  * This command makes the previous window (previous => up the screen) the
  63.  * current window. There arn't any errors, although the command does not do a
  64.  * lot if there is 1 window.
  65.  */
  66. prevwind(f, n)
  67.     {
  68.     register WINDOW *wp1;
  69.     register WINDOW *wp2;
  70.  
  71.     wp1 = wheadp;
  72.     wp2 = curwp;
  73.  
  74.     if (wp1 == wp2)
  75.         wp2 = NULL;
  76.  
  77.     while (wp1->w_wndp != wp2)
  78.         wp1 = wp1->w_wndp;
  79.  
  80.     curwp = wp1;
  81.     curbp = wp1->w_bufp;
  82.     upmode();
  83.     return (TRUE);
  84.     }
  85.  
  86. /*
  87.  * This command moves the current window down by "arg" lines. Recompute the
  88.  * top line in the window. The move up and move down code is almost completely
  89.  * the same; most of the work has to do with reframing the window, and picking
  90.  * a new dot. We share the code by having "move down" just be an interface to
  91.  * "move up". Magic. Bound to "C-X C-N".
  92.  */
  93. mvdnwind(f, n)
  94.     int n;
  95.     {
  96.     return (mvupwind(f, -n));
  97.     }
  98.  
  99. /*
  100.  * Move the current window up by "arg" lines. Recompute the new top line of
  101.  * the window. Look to see if "." is still on the screen. If it is, you win.
  102.  * If it isn't, then move "." to center it in the new framing of the window
  103.  * (this command does not really move "."; it moves the frame). Bound to
  104.  * "C-X C-P".
  105.  */
  106. mvupwind(f, n)
  107.     int n;
  108.     {
  109.     register LINE *lp;
  110.     register int i;
  111.  
  112.     lp = curwp->w_linep;
  113.  
  114.     if (n < 0)
  115.         {
  116.         while (n++ && lp!=curbp->b_linep)
  117.             lp = lforw(lp);
  118.         }
  119.     else
  120.         {
  121.         while (n-- && lback(lp)!=curbp->b_linep)
  122.             lp = lback(lp);
  123.         }
  124.  
  125.     curwp->w_linep = lp;
  126.     curwp->w_flag |= WFHARD;            /* Mode line is OK. */
  127.  
  128.     for (i = 0; i < curwp->w_ntrows; ++i)
  129.         {
  130.         if (lp == curwp->w_dotp)
  131.             return (TRUE);
  132.         if (lp == curbp->b_linep)
  133.             break;
  134.         lp = lforw(lp);
  135.         }
  136.  
  137.     lp = curwp->w_linep;
  138.     i  = curwp->w_ntrows/2;
  139.  
  140.     while (i-- && lp != curbp->b_linep)
  141.         lp = lforw(lp);
  142.  
  143.     curwp->w_dotp  = lp;
  144.     curwp->w_doto  = 0;
  145.     return (TRUE);
  146.     }
  147.  
  148. /*
  149.  * This command makes the current window the only window on the screen. Bound
  150.  * to "C-X 1". Try to set the framing so that "." does not have to move on the
  151.  * display. Some care has to be taken to keep the values of dot and mark in
  152.  * the buffer structures right if the distruction of a window makes a buffer
  153.  * become undisplayed.
  154.  */
  155. onlywind(f, n)
  156. {
  157.         register WINDOW *wp;
  158.         register LINE   *lp;
  159.         register int    i;
  160.  
  161.         while (wheadp != curwp) {
  162.                 wp = wheadp;
  163.                 wheadp = wp->w_wndp;
  164.                 if (--wp->w_bufp->b_nwnd == 0) {
  165.                         wp->w_bufp->b_dotp  = wp->w_dotp;
  166.                         wp->w_bufp->b_doto  = wp->w_doto;
  167.                         wp->w_bufp->b_markp = wp->w_markp;
  168.                         wp->w_bufp->b_marko = wp->w_marko;
  169.                 }
  170.                 free((char *) wp);
  171.         }
  172.         while (curwp->w_wndp != NULL) {
  173.                 wp = curwp->w_wndp;
  174.                 curwp->w_wndp = wp->w_wndp;
  175.                 if (--wp->w_bufp->b_nwnd == 0) {
  176.                         wp->w_bufp->b_dotp  = wp->w_dotp;
  177.                         wp->w_bufp->b_doto  = wp->w_doto;
  178.                         wp->w_bufp->b_markp = wp->w_markp;
  179.                         wp->w_bufp->b_marko = wp->w_marko;
  180.                 }
  181.                 free((char *) wp);
  182.         }
  183.         lp = curwp->w_linep;
  184.         i  = curwp->w_toprow;
  185.         while (i!=0 && lback(lp)!=curbp->b_linep) {
  186.                 --i;
  187.                 lp = lback(lp);
  188.         }
  189.         curwp->w_toprow = 0;
  190.         curwp->w_ntrows = term.t_nrow-1;
  191.         curwp->w_linep  = lp;
  192.         curwp->w_flag  |= WFMODE|WFHARD;
  193.         return (TRUE);
  194. }
  195.  
  196. /*
  197.  * Split the current window. A window smaller than 3 lines cannot be split.
  198.  * The only other error that is possible is a "malloc" failure allocating the
  199.  * structure for the new window. Bound to "C-X 2".
  200.  */
  201. splitwind(f, n)
  202. {
  203.         register WINDOW *wp;
  204.         register LINE   *lp;
  205.         register int    ntru;
  206.         register int    ntrl;
  207.         register int    ntrd;
  208.         register WINDOW *wp1;
  209.         register WINDOW *wp2;
  210.     char *malloc();
  211.  
  212.         if (curwp->w_ntrows < 3) {
  213.                 mlwrite("Cannot split a %d line window", curwp->w_ntrows);
  214.                 return (FALSE);
  215.         }
  216.         if ((wp = (WINDOW *) malloc(sizeof(WINDOW))) == NULL) {
  217.                 mlwrite("Cannot allocate WINDOW block");
  218.                 return (FALSE);
  219.         }
  220.         ++curbp->b_nwnd;                        /* Displayed twice.     */
  221.         wp->w_bufp  = curbp;
  222.         wp->w_dotp  = curwp->w_dotp;
  223.         wp->w_doto  = curwp->w_doto;
  224.         wp->w_markp = curwp->w_markp;
  225.         wp->w_marko = curwp->w_marko;
  226.         wp->w_flag  = 0;
  227.         wp->w_force = 0;
  228.         ntru = (curwp->w_ntrows-1) / 2;         /* Upper size           */
  229.         ntrl = (curwp->w_ntrows-1) - ntru;      /* Lower size           */
  230.         lp = curwp->w_linep;
  231.         ntrd = 0;
  232.         while (lp != curwp->w_dotp) {
  233.                 ++ntrd;
  234.                 lp = lforw(lp);
  235.         }
  236.         lp = curwp->w_linep;
  237.         if (ntrd <= ntru) {                     /* Old is upper window. */
  238.                 if (ntrd == ntru)               /* Hit mode line.       */
  239.                         lp = lforw(lp);
  240.                 curwp->w_ntrows = ntru;
  241.                 wp->w_wndp = curwp->w_wndp;
  242.                 curwp->w_wndp = wp;
  243.                 wp->w_toprow = curwp->w_toprow+ntru+1;
  244.                 wp->w_ntrows = ntrl;
  245.         } else {                                /* Old is lower window  */
  246.                 wp1 = NULL;
  247.                 wp2 = wheadp;
  248.                 while (wp2 != curwp) {
  249.                         wp1 = wp2;
  250.                         wp2 = wp2->w_wndp;
  251.                 }
  252.                 if (wp1 == NULL)
  253.                         wheadp = wp;
  254.                 else
  255.                         wp1->w_wndp = wp;
  256.                 wp->w_wndp   = curwp;
  257.                 wp->w_toprow = curwp->w_toprow;
  258.                 wp->w_ntrows = ntru;
  259.                 ++ntru;                         /* Mode line.           */
  260.                 curwp->w_toprow += ntru;
  261.                 curwp->w_ntrows  = ntrl;
  262.                 while (ntru--)
  263.                         lp = lforw(lp);
  264.         }
  265.         curwp->w_linep = lp;                    /* Adjust the top lines */
  266.         wp->w_linep = lp;                       /* if necessary.        */
  267.         curwp->w_flag |= WFMODE|WFHARD;
  268.         wp->w_flag |= WFMODE|WFHARD;
  269.         return (TRUE);
  270. }
  271.  
  272. /*
  273.  * Enlarge the current window. Find the window that loses space. Make sure it
  274.  * is big enough. If so, hack the window descriptions, and ask redisplay to do
  275.  * all the hard work. You don't just set "force reframe" because dot would
  276.  * move. Bound to "C-X Z".
  277.  */
  278. enlargewind(f, n)
  279. {
  280.         register WINDOW *adjwp;
  281.         register LINE   *lp;
  282.         register int    i;
  283.  
  284.         if (n < 0)
  285.                 return (shrinkwind(f, -n));
  286.         if (wheadp->w_wndp == NULL) {
  287.                 mlwrite("Only one window");
  288.                 return (FALSE);
  289.         }
  290.         if ((adjwp=curwp->w_wndp) == NULL) {
  291.                 adjwp = wheadp;
  292.                 while (adjwp->w_wndp != curwp)
  293.                         adjwp = adjwp->w_wndp;
  294.         }
  295.         if (adjwp->w_ntrows <= n) {
  296.                 mlwrite("Impossible change");
  297.                 return (FALSE);
  298.         }
  299.         if (curwp->w_wndp == adjwp) {           /* Shrink below.        */
  300.                 lp = adjwp->w_linep;
  301.                 for (i=0; i<n && lp!=adjwp->w_bufp->b_linep; ++i)
  302.                         lp = lforw(lp);
  303.                 adjwp->w_linep  = lp;
  304.                 adjwp->w_toprow += n;
  305.         } else {                                /* Shrink above.        */
  306.                 lp = curwp->w_linep;
  307.                 for (i=0; i<n && lback(lp)!=curbp->b_linep; ++i)
  308.                         lp = lback(lp);
  309.                 curwp->w_linep  = lp;
  310.                 curwp->w_toprow -= n;
  311.         }
  312.         curwp->w_ntrows += n;
  313.         adjwp->w_ntrows -= n;
  314.         curwp->w_flag |= WFMODE|WFHARD;
  315.         adjwp->w_flag |= WFMODE|WFHARD;
  316.         return (TRUE);
  317. }
  318.  
  319. /*
  320.  * Shrink the current window. Find the window that gains space. Hack at the
  321.  * window descriptions. Ask the redisplay to do all the hard work. Bound to
  322.  * "C-X C-Z".
  323.  */
  324. shrinkwind(f, n)
  325. {
  326.         register WINDOW *adjwp;
  327.         register LINE   *lp;
  328.         register int    i;
  329.  
  330.         if (n < 0)
  331.                 return (enlargewind(f, -n));
  332.         if (wheadp->w_wndp == NULL) {
  333.                 mlwrite("Only one window");
  334.                 return (FALSE);
  335.         }
  336.         if ((adjwp=curwp->w_wndp) == NULL) {
  337.                 adjwp = wheadp;
  338.                 while (adjwp->w_wndp != curwp)
  339.                         adjwp = adjwp->w_wndp;
  340.         }
  341.         if (curwp->w_ntrows <= n) {
  342.                 mlwrite("Impossible change");
  343.                 return (FALSE);
  344.         }
  345.         if (curwp->w_wndp == adjwp) {           /* Grow below.          */
  346.                 lp = adjwp->w_linep;
  347.                 for (i=0; i<n && lback(lp)!=adjwp->w_bufp->b_linep; ++i)
  348.                         lp = lback(lp);
  349.                 adjwp->w_linep  = lp;
  350.                 adjwp->w_toprow -= n;
  351.         } else {                                /* Grow above.          */
  352.                 lp = curwp->w_linep;
  353.                 for (i=0; i<n && lp!=curbp->b_linep; ++i)
  354.                         lp = lforw(lp);
  355.                 curwp->w_linep  = lp;
  356.                 curwp->w_toprow += n;
  357.         }
  358.         curwp->w_ntrows -= n;
  359.         adjwp->w_ntrows += n;
  360.         curwp->w_flag |= WFMODE|WFHARD;
  361.         adjwp->w_flag |= WFMODE|WFHARD;
  362.         return (TRUE);
  363. }
  364.  
  365. /*
  366.  * Pick a window for a pop-up. Split the screen if there is only one window.
  367.  * Pick the uppermost window that isn't the current window. An LRU algorithm
  368.  * might be better. Return a pointer, or NULL on error.
  369.  */
  370. WINDOW  *
  371. wpopup()
  372. {
  373.         register WINDOW *wp;
  374.  
  375.         if (wheadp->w_wndp == NULL              /* Only 1 window        */
  376.         && splitwind(FALSE, 0) == FALSE)        /* and it won't split   */
  377.                 return (NULL);
  378.         wp = wheadp;                            /* Find window to use   */
  379.         while (wp!=NULL && wp==curwp)
  380.                 wp = wp->w_wndp;
  381.         return (wp);
  382. }
  383.  
  384. scrnextup(f, n)        /* scroll the next window up (back) a page */
  385.  
  386. {
  387.     nextwind(FALSE, 1);
  388.     backpage(f, n);
  389.     prevwind(FALSE, 1);
  390. }
  391.  
  392. scrnextdw(f, n)        /* scroll the next window down (forward) a page */
  393.  
  394. {
  395.     nextwind(FALSE, 1);
  396.     forwpage(f, n);
  397.     prevwind(FALSE, 1);
  398. }
  399.